﻿using System;
using BuildStatusIndicator.Controller.EventBroker;
using BuildStatusIndicator.Controller.Events;
using Microsoft.TeamFoundation.Build.Client;

namespace BuildStatusIndicator.BuildScreen
{
    public class BuildScreenPresenter : IDisposable,IListener<BuildSucceededEvent>, IListener<BuildFailedEvent>,
                                        IListener<BuildPartiallySucceededEvent>, IListener<BuildStoppedEvent>,
                                        IListener<BuildInProgressEvent>, IListener<BuildNotStartedEvent>,
                                        IListener<BuildStatusPolledEvent>
    {
        private readonly IBuildScreenView _view;
        private readonly IBuildStatusEventPublisher _eventPublisher;

        public BuildScreenPresenter(IBuildScreenView view, IBuildStatusEventPublisher eventPublisher)
        {
            _view = view;
            _eventPublisher = eventPublisher;

            _view.AttachPresenter(this);
            _eventPublisher.AddListener(this);
        }

        public IBuildScreenView View
        {
            get { return _view; }
        }

        #region IListener<BuildFailedEvent> Members

        public void Handle(BuildFailedEvent subject)
        {
            SetBuildDetailOnView(subject.BuildDetail);
            _view.SetFailedImage();
        }

        #endregion

        #region IListener<BuildInProgressEvent> Members

        public void Handle(BuildInProgressEvent subject)
        {
            SetBuildDetailOnView(subject.BuildDetail);
            _view.SetInProgressImage();
        }

        #endregion

        #region IListener<BuildNotStartedEvent> Members

        public void Handle(BuildNotStartedEvent subject)
        {
            SetBuildDetailOnView(subject.BuildDetail);
            _view.SetNotStartedImage();
        }

        #endregion

        #region IListener<BuildPartiallySucceededEvent> Members

        public void Handle(BuildPartiallySucceededEvent subject)
        {
            SetBuildDetailOnView(subject.BuildDetail);
            _view.SetPartiallySucceededImage();
        }

        #endregion

        #region IListener<BuildStoppedEvent> Members

        public void Handle(BuildStoppedEvent subject)
        {
            SetBuildDetailOnView(subject.BuildDetail);
            _view.SetStoppedImage();
        }

        #endregion

        #region IListener<BuildSucceededEvent> Members

        public void Handle(BuildSucceededEvent subject)
        {
            SetBuildDetailOnView(subject.BuildDetail);
            _view.SetSucceededImage();
        }

        #endregion

        private void SetBuildDetailOnView(IBuildDetail detail)
        {
            _view.SetStatus(detail.BuildServer.GetDisplayText(detail.Status));
            _view.SetBuildNumber(detail.BuildNumber);
            _view.SetRequestedFor(UserNameFormatter.GetFriendlyName(detail.RequestedFor, null));
            _view.SetFinishTime(detail.FinishTime.Equals(DateTime.MinValue)
                                    ?
                                        ""
                                    : detail.FinishTime.ToString());
        }

        public void ViewLoad()
        {
            //throw new NotImplementedException();
        }

        public void ViewClosing()
        {
            //throw new NotImplementedException();
        }

        #region Implementation of IListener<BuildStatusPolledEvent>

        public void Handle(BuildStatusPolledEvent subject)
        {
            _view.SetLastUpdated(subject.PollTime.ToLongTimeString());
        }

        #endregion

        #region Implementation of IDisposable

        public void Dispose()
        {
            _eventPublisher.RemoveListener(this);
        }

        #endregion
    }
}